'''
Created on 2011-7-29

@author: Johnny
'''
import copy
import time
import os

from PyQt4 import QtCore

from util.util import Util
from util.exceloperator import ExcelOperator
from util.xmlparse import XmlParser
from util.serverconn import connect
from threadmanager import ThreadProxy
from configuration import Configuration
from reporter.reporter import Reporter

class Controller(QtCore.QObject):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
        QtCore.QObject.__init__(self, parent = None)
        self.root = Util.get_root_path()
        self.config = Configuration(self.root + '/conf.ini')
        self.config.get_all()
    
    def start_servers(self):
        self.host = self.config.host_list
        self.host = copy.deepcopy(connect(self.host, self.root, 
                    self.config.serverpath, self.config.profilepath))
        time.sleep(3)
    
    def update_conf(self):
        self.config.get_all()
    
    def get_cases(self, filename, priority='All'):
        '''
        Import case to xml file.
        @params:
        filename -- excel file to read
        priority -- case priority
        '''
        print 'controller -- get_cases '
        filepath = self.root + "/Cases/" + filename
        if os.path.exists(filepath):
            excel = ExcelOperator(filepath)
            xml_parser = XmlParser(self.root + "/Cases/case.xml")
        else:
            print 'getcase -- failed: file not found'
            return False
        
        try:
            self.case_list = excel.get_case_list(priority)
            xml_parser.write_xml_by_excel(self.case_list)
            excel.close()
            print 'Get cases done!'
            return True
        except Exception, e:
            print 'getcase -- ' + str(e)
            excel.close()
            return False
    
    def read_cases(self):
        '''
        Get case from xml file.
        '''
        print 'controller -- read_cases '
        xml_parser = XmlParser(self.root + "/Cases/case.xml")
        try:
            self.cases = xml_parser.read_case_from_xml()
            return True
        except:
            return False
    
    def set_dest(self, dest):
        print 'controller -- set destination site'
        self.dest = dest
        print 'controller -- set destination done: ' + dest
    
    def report_output(self):
        print "controller -- report_output "
        reporter = Reporter(self.config)
        reporter.report_database(self.cases)
        
    def update_ui(self):
        print "controller -- update_ui "
        self.emit(QtCore.SIGNAL("updateui"))
        
    def test(self):
        print 'controller -- test '
        self.update_conf()
        self.set_dest(self.config.url)
        
        self.start_servers()
        if self.read_cases():
            self.proxy = ThreadProxy(self.host, 1, self.dest, self.cases)
        
            self.connect(self.proxy, QtCore.SIGNAL("finished()"), 
                         self.update_ui)
            self.connect(self.proxy, QtCore.SIGNAL("terminated()"), 
                         self.update_ui)
            self.connect(self.proxy, QtCore.SIGNAL("finished()"),
                         self.report_output)
            self.proxy.start()
        
        